#include "py_editor_extension.hpp"
#include "py_function_extension.hpp"
#include "scripting/scripting_module.h"
#include "utils/log.hpp"

#include <Python.h>
#include <import.h>
#include <pytypedefs.h>

#include "py_app_module.h"

bool ModuleInit()
{
    L_INFO("Python Scripting Module Init...");
    PyImport_AppendInittab("app", &InitAppModule);
    Py_Initialize();
    return true;
}

void ModuleCleanup()
{
    L_INFO("Python Scripting Module Cleanup...");
    Py_Finalize();
}

IScript* BuildScript(const ScriptBuildInfo *info)
{
    RC_ERROR(nullptr, !(info && info->Valid()), "Invalid Script Build Info Passed!!!");
    RC_ERROR(nullptr, strchr(info->code, '\t'), "Python Code Must Use Space For Indent!");
    if (strcmp(info->usage, "editor") == 0) {
        return PythonEditorScript::create(EditorTargetFromString(info->usage_extra), info->title, info->code, info->file_path);
    } else if (strcmp(info->usage, "function") == 0) {
        return PythonFunctionScript::create(info->title, info->code, info->file_path);
    }
    return nullptr;
}
